﻿
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform.SG;
using Hjt212Parser;

namespace Biz.Process
{
    /// <summary>
    /// 负责处理响应指令：9011、9012、9014
    /// </summary>
    public static class ProcessorResponse
    {
        /// <summary>
        /// 未处理：0；
        /// 正在下行：1；
        /// 下行成功：2；
        /// 下行失败：3；
        /// 执行成功：4；
        /// 执行失败：5.
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process9011(PlatformXpfContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var qn = Parser.GetStringByKey("QN", content);
            //var qnRtn = Parser.GetStringByKey("QnRtn", content);
            var cmds = pCtx.阀门控制.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.企业排口ID == port.ID)
                .Where(t => t.QN.Equals(qn)).ToList();
            foreach (var cmd in cmds)
            {
                cmd.执行结果标志ID = 602;
                pCtx.SaveChanges();
            }
        }
        public static void Process9012(PlatformXpfContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var qn = Parser.GetStringByKey("QN", content);
            var exeRtn = Parser.GetStringByKey("ExeRtn", content);
            var cmds = pCtx.阀门控制.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.企业排口ID == port.ID)
                .Where(t => t.QN.Equals(qn)).ToList();
            foreach (var cmd in cmds)
            {
                switch (exeRtn)
                {
                    case "1":
                        cmd.执行结果标志ID = 603;
                        break;
                    case "2":
                        cmd.执行结果标志ID = 604;
                        break;
                    case "4":
                        cmd.执行结果标志ID = 605;
                        break;
                }
                pCtx.SaveChanges();
            }
        }
        public static void Process9014(PlatformXpfContext pCtx, string content)
        {

        }
    }
}
